package com.alks.function.service.serviceImpl.finance.salary;

import com.alks.common.aop.AutoPageAop;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.beanutils.ObjectUtils;
import com.alks.entity.data.entity.PcSalaryEndMonth;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.data.request.finance.salary.PcBonusEndMonthRequest;
import com.alks.function.mapper.finance.salary.PayrollClosureMapper;
import com.alks.function.mapper.finance.salary.PersonnelMapper;
import com.alks.function.service.finance.salary.PayrollClosureService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
public class PayrollClosureServiceImpl implements PayrollClosureService {

    @Autowired
    private PayrollClosureMapper payrollClosureMapper;

    @Resource
    private PersonnelMapper personnelMapper;

    /**
     * 查询：工资封账月份明细
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo getSealedMonthSalaryDetails(PcBonusEndMonthRequest request) {
        String companyId = UserIdThread.get().getCompanyId();
        request.setCompanyId(companyId);
        List<PcSalaryEndMonth> detailsList = payrollClosureMapper.getSealedMonthSalaryDetails(request);

        PageInfo list = new PageInfo(detailsList);
        HashMap map = new HashMap<>();
        map.put("inboundDetailsList", detailsList);
        map.put("total", list.getTotal());
        return ResponseInfo.ok(map);
    }

    /**
     * 添加数据：工资封账月份明细
     * @param request
     * @return
     */
    @Override
    public ResponseInfo addSealedMonthSalaryDetails(PcBonusEndMonthRequest request) {
        //1》输入判断
        if(request.getBonusYM() == null || request.getBonusYM() == "" ) {
            return ResponseInfo.error("请输入月份！");
        }
        if(request.getBonusYM().length() > 6){
            return ResponseInfo.error("月份长度不能超过6位！");
        }

        //3》判断有没有重复
        String companyId = UserIdThread.get().getCompanyId();
        String userName = UserIdThread.get().getUserName();
        String key = companyId + request.getBonusYM();
        request.setCompanyId(companyId);
        List<PcSalaryEndMonth> detailsList = payrollClosureMapper.getSealedMonthSalaryDetails(request);
        if(ObjectUtils.isNotEmpty(detailsList)){
            return ResponseInfo.error("月份已存在！");
        }

        //2》计算BonusYM为无效月份，且能继续进行
        if(request.getBonusYM().length() != 6){
            return ResponseInfo.error("无效月份！");
        }

        String salaryFlag = personnelMapper.getSalaryFlag(request.getBonusYM(),companyId);
        if (!"Y".equals(salaryFlag)){
            throw new ServiceErrorException(request.getBonusYM() + "月份工资未检核,不能封账处理!");
        }
        //输入不符合时间规律
        if(request.getBonusYM().length() == 6){
            String s = request.getBonusYM().substring(4, 6);
            int month = Integer.parseInt(request.getBonusYM().substring(4, 6));
            if(month > 12 && month < 1){
                return ResponseInfo.error("无效月份！");
            }
        }

        //4》添加新的
        PcSalaryEndMonth pcSalaryEndMonth = new PcSalaryEndMonth();
        BeanUtils.copyProperties(request, pcSalaryEndMonth);
        pcSalaryEndMonth.setSysUser(userName);
        pcSalaryEndMonth.setCompanyId(companyId);
        int count = payrollClosureMapper.addSealedMonthSalaryDetails(pcSalaryEndMonth);
        if(count > 0){
            return ResponseInfo.ok("新增成功！");
        }else {
            return ResponseInfo.error("新增失败！");
        }
    }

    /**
     * 删除：工资封账月份明细
     * @param request
     * @return
     */
    @Override
    public ResponseInfo deleteSealedMonthSalaryDetails(PcBonusEndMonthRequest request) {
        String companyId = UserIdThread.get().getCompanyId();
        String userName = UserIdThread.get().getUserName();
        PcSalaryEndMonth pcSalaryEndMonth = new PcSalaryEndMonth();
        BeanUtils.copyProperties(request, pcSalaryEndMonth);
        pcSalaryEndMonth.setSysUser(userName);
        pcSalaryEndMonth.setCompanyId(companyId);
        int count = payrollClosureMapper.deleteSealedMonthSalaryDetails(pcSalaryEndMonth);
        if(count > 0){
            return ResponseInfo.ok("删除成功！");
        }else {
            return ResponseInfo.error("删除失败！");
        }
    }
}